//
//  CodeViewController.m
//  图片故事
//
//  Created by 爽 on 16/3/3.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "CodeViewController.h"
#import "Header.h"
#import "FinishViewController.h"
//#import <SMS_SDK/SMSSDK.h>
#import "CountDownServer.h"
#import "MBProgressHUD+PKX.h"
#import "ForgetViewController.h"
@interface CodeViewController ()
@property (nonatomic,strong)UITextField *FunnelTextField;
@property (nonatomic,strong)UIButton *Button;
@property (nonatomic,strong)UIButton *AniamButton;
@property (nonatomic,strong)UILabel *Label;
@end

@implementation CodeViewController
#pragma mark -- 懒加载
-(UILabel *)Label {
    if (!_Label) {
        _Label = [[UILabel alloc] initWithFrame:CGRectMake(My_Width(5), My_Height(85), SCREEN_W, My_Height(60))];
        _Label.text = [NSString stringWithFormat:@"我们已给你的手机号码%@发送了一条验证短信。",_phoneNumStr];
        _Label.textColor = [UIColor grayColor];
        _Label.font = [UIFont boldSystemFontOfSize:16.0];
        _Label.numberOfLines = 0;
       
    }
    return _Label;
}
-(UIButton *)AniamButton {
    if (!_AniamButton) {
        _AniamButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _AniamButton.frame = CGRectMake(My_Width(20), My_Height(240), SCREEN_W -My_Width(40), My_Height(50));
        [_AniamButton setTitle:@"下一步" forState:UIControlStateNormal];
        _AniamButton.backgroundColor = [UIColor colorWithRed:102 / 255.0 green:214 / 255.0 blue:228 / 255.0 alpha:1];
        [_AniamButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _AniamButton.layer.cornerRadius = 5;
        [_AniamButton addTarget:self action:@selector(registerbutton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _AniamButton;
}

-(UIButton *)Button {
    if (!_Button) {
        _Button = [UIButton buttonWithType:UIButtonTypeSystem];
        _Button.frame = CGRectMake(SCREEN_W - My_Width(90), My_Height(160), My_Width(90), My_Height(50));
        _Button.backgroundColor = [UIColor colorWithRed:102 / 255.0 green:214 / 255.0 blue:228 / 255.0 alpha:1];
        [_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_Button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];

    }
    return _Button;
}
-(UITextField *)FunnelTextField {
    if (!_FunnelTextField) {
        _FunnelTextField = [[UITextField alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(160), SCREEN_W - My_Width(90), My_Height(50))];
        _FunnelTextField.placeholder = @"请输入验证码";
        _FunnelTextField.backgroundColor = Btn_Color;
        [_FunnelTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _FunnelTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(0), My_Width(20), My_Height(0))];
        [_FunnelTextField setTextColor:[UIColor grayColor]];
        _FunnelTextField.leftView = leftView;
        _FunnelTextField.leftViewMode = UITextFieldViewModeAlways;
        
    }
    return _FunnelTextField;
}
- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.backgroundColor = Bg_Color;
    
    [self initNavigation];

    [self loadWidgetView];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(countDownUpdate:) name:@"CountDownUpdate" object:nil];
    
    if (![CountDownServer isCountDowning:@"CountDownForVerifyCode"]) {
        
        [CountDownServer startCountDown:60 identifier:@"CountDownForVerifyCode"];
    }
}

- (void)initNavigation {
    self.title = @"填写验证码";
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
}

- (void)loadWidgetView {
    
    [self.view addSubview:self.FunnelTextField];
    [self.view addSubview:self.Button];
    [self.view addSubview:self.AniamButton];
    [self.view addSubview:self.Label];
}

#pragma mark -- 点击事件

-(void)registerbutton {
    
    /*
    [SMSSDK commitVerificationCode:self.FunnelTextField.text phoneNumber:_phoneNumStr zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
        if (!error) {
            if ([_identifier isEqualToString:@"forget"]) {
                ForgetViewController *forget = [[ForgetViewController alloc] init];
                forget.phoneNum = _phoneNumStr;
                [self.navigationController pushViewController:forget animated:YES];
                
            } else if ([_identifier isEqualToString:@"regist"]){
                
                FinishViewController *FN = [[FinishViewController alloc]init];
                FN.phoneNum = _phoneNumStr;
                [self.navigationController pushViewController:FN animated:YES];
            }
        } else {
            
            [MBProgressHUD showError:@"验证码错误"];
        }
    }];
     */
}

- (void)action_backItmeClickEvent {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnClicked {
    /*
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNumStr zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        if (error) {
            [MBProgressHUD showError:@"验证码获取失败"];
        }
    }];
     */
    [CountDownServer startCountDown:60 identifier:@"CountDownForVerifyCode"];
    
}
- (void)countDownUpdate:(NSNotification *)noti
{
    NSString *identifier = [noti.userInfo objectForKey:@"CountDownIdentifier"];
    if ([identifier isEqualToString:@"CountDownForVerifyCode"]) {
        NSNumber *n = [noti.userInfo objectForKey:@"SecondsCountDown"];
        
        [self performSelectorOnMainThread:@selector(updateVerifyCodeCountDown:) withObject:n waitUntilDone:YES];
    }
}

- (void)updateVerifyCodeCountDown:(NSNumber *)num{
    if ([num integerValue] == 0){
        [self.Button setTitle:@"重新发送" forState:UIControlStateNormal];
        self.Button.enabled = YES;
    }else{
        [self.Button setTitle:[NSString stringWithFormat:@"%@",num] forState:UIControlStateNormal];
        self.Button.enabled = NO;
    }
}

@end
